package com.cfushn.ddd.catchlog.test;

import com.cfushn.ddd.application.dto.res.ResDTO;
import com.cfushn.ddd.catchlog.LogCatch;
import com.cfushn.ddd.catchlog.LogCatchAspect;
import com.cfushn.ddd.exception.BizException;
import com.cfushn.ddd.exception.SysException;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * Demo
 *
 * @author Frank Zhang
 * @date 2020-11-10 11:19 AM
 */
@Configuration
@LogCatch
public class Demo implements ApplicationContextAware {

    static ApplicationContext applicationContext;

    public void doSomething() {
        System.out.println("Doing something");
        LogCatchAspect catchAndLog = applicationContext.getBean(LogCatchAspect.class);
        System.out.println(catchAndLog);
        doSomethingInner();
    }

    private void doSomethingInner() {
        System.out.println("doSomethingInner");
    }

    public DemoResponse execute(Request request) {
        System.out.println("executing request");
        return new DemoResponse(request.name, true);
    }

    public DemoResponse executeWithExceptionAndDemoResponse() {
        if (true) {
            throw new RuntimeException("executeWithExceptionAndDemoResponse");
        }
        return null;
    }

    public ResDTO executeWithResponse(Request request) {
        DemoResponse demoResponse = new DemoResponse("Jack Ma", true);
        return ResDTO.succeed(demoResponse);
    }

    public ResDTO executeWithExceptionAndResponse() {
        if (true) {
            throw new RuntimeException("execute With Exception And Response");
        }
        return null;
    }

    public void executeWithVoid() {
        System.out.println("execute with void");
    }

    public void executeWithExceptionAndVoid() {
        if (true) {
            throw new BizException("execute With Exception And Void");
        }
    }

    public ResDTO executeWithBizExceptionAndResponse() {
        if (true) {
            throw new BizException("execute With BizException And Response");
        }
        return null;
    }

    public ResDTO executeWithSysExceptionAndResponse() {
        if (true) {
            throw new SysException("execute With SysException And Response");
        }
        return null;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static class Request {

        public String name;
        public int age;
    }

    @AllArgsConstructor
    public static class DemoResponse extends ResDTO {

        public String name;
        public boolean isSuccess;

        public DemoResponse() {

        }
    }
}
